/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.belfalas.simy86.gui;

import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.awt.image.BufferedImage;
import java.nio.ByteBuffer;
import javax.swing.JFrame;
import javax.swing.Timer;
import org.belfalas.simy86.Y86;

/**
 *
 * @author belfalas
 */
public class Window2 extends Component {
    public static Timer timer;

    @Override
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        BufferedImage br = new BufferedImage(320, 240, BufferedImage.TYPE_INT_RGB);
        ByteBuffer bb = Y86.memoria.getVideoMemory();
        int Y = 0;
        while (bb.hasRemaining() && (bb.capacity()-bb.position() >= 4)){
            int X = (bb.position() - (4*Y))/960;            
            int color = bb.getInt();
            try {
                br.setRGB(X, Y, color);
            } catch (Exception ex){
                System.out.println("X: "+X+" Y:"+Y);
            }
            Y = (Y+1)%br.getHeight();
        }
        g2d.drawImage(br, 0, 0, this);
    }

    public void start() {
        this.setSize(320, 240);
        this.setLocation(0, 0);
        JFrame frame = new JFrame();
        frame.setSize(325, 270);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        frame.add(this);

        frame.addWindowListener(new WindowListener(){

            public void windowOpened(WindowEvent e) {
                
            }

            public void windowClosing(WindowEvent e) {
                timer.stop();
            }

            public void windowClosed(WindowEvent e) {
            }

            public void windowIconified(WindowEvent e) {
            }

            public void windowDeiconified(WindowEvent e) {
            }

            public void windowActivated(WindowEvent e) {
            }

            public void windowDeactivated(WindowEvent e) {
            }
            
        });
        
        this.setVisible(true);
        frame.setVisible(true);
        

    }
    
    public void redraw(){
        this.repaint();
    }
}
